﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace Shared.Method
{
    public class Encrypt
    {
        #region DES3加密
        //24位密钥
        private static string a_strKey = "u%^un2ye31>Bc%|XXrpR?#MU";

        /// <summary>
        /// 加密方法
        /// </summary>
        /// <param name="a_strString"></param>
        /// <returns></returns>
        public static string Encrypt3DES(string inputStr)
        {
            string outputStr = "";

            if (!"".Equals(inputStr))
            {
                TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
                DES.Key = Encoding.UTF8.GetBytes(a_strKey);
                DES.Mode = CipherMode.ECB;
                ICryptoTransform DESEncrypt = DES.CreateEncryptor();

                try
                {
                    byte[] Buffer = Encoding.UTF8.GetBytes(inputStr);
                    outputStr = Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    return string.Empty;
                }
            }
            return outputStr;
        }

        /// <summary>
        /// 解密方法
        /// </summary>
        /// <param name="a_strString"></param>
        /// <returns></returns>
        public static string Decrypt3DES(string inputStr)
        {
            string outputStr = "";

            if (!"".Equals(inputStr))
            {
                TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
                DES.Key = Encoding.UTF8.GetBytes(a_strKey);
                DES.Mode = CipherMode.ECB;
                DES.Padding = PaddingMode.PKCS7;
                ICryptoTransform DESDecrypt = DES.CreateDecryptor();
                try
                {
                    byte[] Buffer = Convert.FromBase64String(inputStr);
                    outputStr = Encoding.UTF8.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    return string.Empty;
                }
            }
            return outputStr;
        }
        #endregion

        #region SHA加密

        /// <summary>
        /// SHA256 加密
        /// </summary>
        /// <param name="sourceString">原字符串</param>
        /// <returns>加密后字符串</returns>
        public static string SHA256_Encrypt(string sourceString)
        {
            byte[] data = Encoding.UTF8.GetBytes(sourceString);
            SHA256 shaM = SHA256.Create();
            byte[] result = shaM.ComputeHash(data);
            StringBuilder EnText = new StringBuilder();
            foreach (byte iByte in result)
            {
                EnText.AppendFormat("{0:x2}", iByte);
            }
            return EnText.ToString();
        }

        /// <summary>
        /// SHA384 加密
        /// </summary>
        /// <param name="sourceString">原字符串</param>
        /// <returns>加密后字符串</returns>
        public static string SHA384_Encrypt(string sourceString)
        {
            byte[] data = Encoding.UTF8.GetBytes(sourceString);
            SHA384 shaM = SHA384.Create();
            byte[] result = shaM.ComputeHash(data);
            StringBuilder EnText = new StringBuilder();
            foreach (byte iByte in result)
            {
                EnText.AppendFormat("{0:x2}", iByte);
            }
            return EnText.ToString();
        }

        /// <summary>
        /// SHA512_加密
        /// </summary>
        /// <param name="sourceString">原字符串</param>
        /// <returns>加密后字符串</returns>
        public static string SHA512_Encrypt(string sourceString)
        {
            byte[] data = Encoding.UTF8.GetBytes(sourceString);
            SHA512 shaM = new SHA512Managed();
            byte[] result = shaM.ComputeHash(data);
            StringBuilder EnText = new StringBuilder();
            foreach (byte iByte in result)
            {
                EnText.AppendFormat("{0:x2}", iByte);
            }
            return EnText.ToString();
        }

        #endregion

        #region MD5
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="strPwd">原字符串</param>
        /// <returns>加密后字符串</returns>
        public static string GetMD5(string strPwd)
        {
            //MD5 对象创建的两种方式
            //MD5 md5 = MD5.Create();
            MD5 md5 = new MD5CryptoServiceProvider();
            //将输入的密码转换成字节数组
            byte[] bPwd = Encoding.UTF8.GetBytes(strPwd);
            //计算指定字节数组的哈希值
            byte[] bMD5 = md5.ComputeHash(bPwd);
            //释放加密服务提供类的所有资源
            md5.Clear();
            StringBuilder sbMD5Pwd = new StringBuilder();
            for (int i = 0; i < bMD5.Length; i++)
            {
                //将每个字节数据转换为2位的16进制的字符
                sbMD5Pwd.Append(bMD5[i].ToString("x2"));
            }
            return sbMD5Pwd.ToString();
        }
        #endregion

        #region 自定义加密方式

        public static string GetCheckCode(string one, string two)
        {
            if (string.IsNullOrEmpty(one) || string.IsNullOrEmpty(two)) return string.Empty;
            try
            {
                return SHA256_Encrypt(GetKeyMethodOne(one) + GetKeyMethodTwo(two));
            }
            catch
            {
                return string.Empty;
            }
        }

        public static string GetKeyMethodOne(string word)
        {
            if (string.IsNullOrEmpty(word)) return string.Empty;
            try
            {
                return SHA256_Encrypt(word).Substring(15, 28);
            }
            catch
            {
                return string.Empty;
            }
        }

        public static string GetKeyMethodTwo(string word)
        {
            if (string.IsNullOrEmpty(word)) return string.Empty;
            try
            {
                return SHA512_Encrypt(word).Substring(57, 39);
            }
            catch
            {
                return string.Empty;
            }
        }

        #endregion
    }
}
